package com.dhcc.sds.base.controller;


import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.base.entity.SdsDicEntity;
import com.dhcc.sds.base.service.ISdsDicService;


/**
 * 基础字典
 * 
 * @ClassName: SdsDicController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:10:53
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsdic")
public class SdsDicController extends BaseController {

	private String PREFIX = "/backend/sds/sdsdic/";

	@Autowired
	private ISdsDicService sdsDicService;
	

    /**
     * 跳转到基础字典列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sdsdic.html";
    }
	
    /**
     * 跳转到新增基础字典
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsdic_add.html";
    }
	
	/**
     * 跳转到修改基础字典
     */
    @RequestMapping("/to_update/{sdsDicId}")
    public String toUpdate(@PathVariable Long sdsDicId, Model model) {
        SdsDicEntity sdsDic = this.sdsDicService.selectById(sdsDicId);
        model.addAttribute("sdsDic",sdsDic);
        return PREFIX + "sdsdic_edit.html";
    }

    /**
     * 获取基础字典列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsDicEntity> page = new PageFactory<SdsDicEntity>().defaultPage();
        List<SdsDicEntity> result = this.sdsDicService.page(page,params);
        page.setRecords((List<SdsDicEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增基础字典
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsDicEntity sdsDic) {
        if (CommonUtil.isOneEmpty(sdsDic)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsDic.insert();
        return R.ok();
    }

    /**
     * 删除基础字典
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsDicId) {
	
        this.sdsDicService.deleteById(sdsDicId);

        return R.ok();
    }

    /**
     * 修改基础字典
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsDicEntity sdsDic) {
        if (CommonUtil.isOneEmpty(sdsDic)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsDicEntity old = this.sdsDicService.selectById(sdsDic.getId());

        old.setDicType(sdsDic.getDicType());
        old.setDicCode(sdsDic.getDicCode());
        old.setDicDesc(sdsDic.getDicDesc());
        old.setIsActive(sdsDic.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 基础字典置为无效
     */
    @RequestMapping(value = "/cancel")
    @ResponseBody
    public Object cancel(@RequestParam Long sdsDicId) {
        SdsDicEntity entity = new SdsDicEntity();
        entity.setId(sdsDicId);
        entity.setIsActive(0);

        this.sdsDicService.updateById(entity);

        return R.ok();
    }
}
